#include "stdafx.h"
#include "Logger.h"
#include "Form1.h"

#include <windows.h>	
#using <System.dll>
#using <System.Windows.Forms.dll>
#using <System.Drawing.dll>

using namespace System::Windows::Forms;

void Logger::InsertErrorMessage(System::String^ errorText){
	/// rozwiazanie problemu dostepu do kontrolki z roznych watkow :)
	/// http://msdn.microsoft.com/en-us/library/ms171728.aspx
	if (bError){
		if (listView->InvokeRequired){
			SetTextDelegate^ d = gcnew SetTextDelegate(&Logger::InsertErrorMessage);
            listView->Invoke(d, gcnew array<Object^> { errorText });
		} else {			
			InsertLog(errorText, ErrorType::eError);
		}
	}
}
//-----------------------------------------------------------------------------------------------
void Logger::InsertWarningMessage(System::String^ errorText){
	if (bWarning){
		if (listView->InvokeRequired){
			SetTextDelegate^ d = gcnew SetTextDelegate(&Logger::InsertWarningMessage);
            listView->Invoke(d, gcnew array<Object^> { errorText });
		} else {
			InsertLog(errorText, ErrorType::eWarning);
		}
	}
}
//-----------------------------------------------------------------------------------------------
void Logger::InsertInfoMessage(System::String^ errorText){
	if (bInfo){
		if (listView->InvokeRequired){
			SetTextDelegate^ d = gcnew SetTextDelegate(&Logger::InsertInfoMessage);
            listView->Invoke(d, gcnew array<Object^> { errorText });
		} else {
			InsertLog(errorText, ErrorType::eMessage);
		}
	}
}
//-----------------------------------------------------------------------------------------------
void Logger::InsertDebugMessage(System::String^ errorText){
	if (bDebug){
		if (listView->InvokeRequired){
			SetTextDelegate^ d = gcnew SetTextDelegate(&Logger::InsertDebugMessage);
            listView->Invoke(d, gcnew array<Object^> { errorText });
		} else {
			InsertLog(errorText, ErrorType::eDebug);
		}
		
	}
}
//-----------------------------------------------------------------------------------------------
void Logger::InsertCollisionMessage(System::String^ errorText){
	if (bCollision){
		if (listView->InvokeRequired){
			SetTextDelegate^ d = gcnew SetTextDelegate(&Logger::InsertCollisionMessage);
            listView->Invoke(d, gcnew array<Object^> { errorText });
		} else {
			InsertLog(errorText, ErrorType::eCollision);
		}
	}
}
//-----------------------------------------------------------------------------------------------

void Logger::InsertLog(System::String^ errorText, Logger::ErrorType eType)
{

	System::DateTime dt = System::DateTime::Now;
	System::String^ sErrorTime = dt.ToLongTimeString() + "." + dt.Millisecond.ToString("D3");
	ListViewItem^ item = gcnew ListViewItem();
	item->Text = sErrorTime;
	DeleteRedundantItems();
	item = listView->Items->Insert(0, item);			

	switch (eType)
	{
	case Logger::ErrorType::eError:
		item->BackColor = Color::Crimson;
		break;
	case Logger::ErrorType::eWarning:
		item->BackColor = Color::Yellow;
		break;
	case Logger::ErrorType::eMessage:
		item->BackColor = Color::MediumPurple;
		break;
	case Logger::ErrorType::eDebug:
		item->BackColor = Color::AliceBlue;
		break;
	case Logger::ErrorType::eCollision:
		item->BackColor = Color::Red;
		break;
	}

	item->SubItems->Add(errorText);
}
//-----------------------------------------------------------------------------------------------
void Logger::DeleteRedundantItems(){
	if (listView->Items->Count > itemsTreshold)
		listView->Items->RemoveAt(listView->Items->Count - 1);
}